import request from "@/utils/request";

export function getAllScene() {
  return request.get({
    url: "/api/Postcard/getAllScene",
  });
}
export function getAllStyle() {
  return request.get({
    url: "/api/Postcard/getAllStyle",
  });
}
export function jssdkConfig(data) {
  return request.post({
    url: "/api/Postcard/jssdkConfig",
    data,
  });
}
export function getAllSize() {
  return request.get({
    url: "/api/Postcard/getAllSize",
  });
}
export function createAiPic(data) {
  return request.post(
    {
      url: "/api/Postcard/createAiPic",
      data,
    },
    { show: false }
  );
}
export function saveShare(data) {
  return request.post({
    url: "/api/Postcard/saveShare",
    data,
  });
}
export function printPic(data) {
  return request.get(
    {
      url: "/api/Postcard/printPic",
      data,
    },
    { showAll: true }
  );
}
export function downloadImg(data) {
  return request.get({
    url: "/api/Postcard/download",
    data,
    responseType: "arraybuffer",
  });
}
export function getPicToWall(data) {
  return request.get({
    url: "/api/Postcard/getPicToWall",
    data,
  });
}

export function downloadBinaryFileH5(url, filename) {
  uni.request({
    url: url,
    method: "GET",
    responseType: "arraybuffer",
    success: (response) => {
      // 将 ArrayBuffer 转换为 Blob
      const blob = new Blob([response.data]);

      // 创建临时 URL
      const url = window.URL.createObjectURL(blob);

      // 创建隐藏的 <a> 元素
      const a = document.createElement("a");
      a.style.display = "none";
      a.href = url;
      a.download = filename;

      // 触发点击事件进行下载
      document.body.appendChild(a);
      a.click();

      // 清理临时 URL 和 <a> 元素
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url);
    },
    fail: (error) => {
      console.error("下载文件失败:", error);
    },
  });
}
